package com.yunyao.dao.service.system.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunyao.common.constant.enums.DataDeletedStatus;
import com.yunyao.dao.mapper.BamuSysDeptMapper;
import com.yunyao.dao.mapper.SysRoleMapper;
import com.yunyao.dao.mapper.UserMapper;
import com.yunyao.dao.model.BamuSysDept;
import com.yunyao.dao.model.SysRole;
import com.yunyao.dao.model.User;
import com.yunyao.dao.service.system.IUserService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author xingjishuai
 * @since 2024-07-02
 */
@Service("yunyaoSysUserService")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private BamuSysDeptMapper bamuSysDeptMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public IPage<User> getDeptList(Long pageIndex, Long pageSize, String deptId, String realName, String account) {
        String newAncestors = getAncestors(deptId);
        long deptCount = baseMapper.deptCount(newAncestors, realName,account);
        long index = pageIndex <= 1 ? 0 : (pageIndex - 1) * pageSize;
        List<User> bamuUserList = baseMapper.selectByDeptOrRealName(newAncestors, realName,account,index
                , pageSize);
        IPage<User> page = new Page<>(pageIndex, pageSize, deptCount);
        page.setRecords(bamuUserList);
        page.setPages(deptCount % pageSize == 0 ? deptCount / pageSize : deptCount / pageSize + 1);
        return page;
    }

    @Override
    public List<User> listUserAdministrator() {
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRole::getIsDeleted, DataDeletedStatus.NO.getType());
        wrapper.eq(SysRole::getRoleAlias, "administrator");
        SysRole sysRole = sysRoleMapper.selectOne(wrapper);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getIsDeleted, DataDeletedStatus.NO.getType());
        queryWrapper.like(User::getRoleId, sysRole.getId());
        return list(queryWrapper);
    }

    @Override
    public List<User> listByUserId(List<String> userIdList) {
        if (CollectionUtils.isEmpty(userIdList)) {
            return List.of();
        }
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<User>()
                .in(User::getId, userIdList)
                .eq(User::getIsDeleted, DataDeletedStatus.NO.getType());
        return list(wrapper);
    }

    @Override
    public String getRealNameByUserId(String userId) {
        User user = userMapper.selectById(userId);
        if (user!=null) {
            return user.getRealName();
        }else {
            return "";
        }
    }

    @Override
    public User getByIdNoDel(String id) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getId, id).eq(User::getIsDeleted, DataDeletedStatus.NO.getType());
        return getOne(wrapper);
    }

    @Override
    public User getByAccount(String account, String tenantId) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getAccount, account)
                .eq(User::getTenantId, tenantId)
                .eq(User::getIsDeleted, DataDeletedStatus.NO.getType());
        return getOne(wrapper);
    }

    private String getAncestors(String deptId) {
        if (StringUtils.isBlank(deptId)) {
            return StringUtils.EMPTY;
        }
        LambdaQueryWrapper<BamuSysDept> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BamuSysDept::getId, deptId);
        wrapper.eq(BamuSysDept::getIsDeleted, DataDeletedStatus.NO.getType());
        BamuSysDept bamuSysDept = bamuSysDeptMapper.selectOne(wrapper);
        if (bamuSysDept == null) {
            return StringUtils.EMPTY;
        }
        return bamuSysDept.getAncestors() + bamuSysDept.getId() + ",";
    }
}
